package com.lzx.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.article.entity.Articles;
import com.lzx.article.entity.Collect;
import com.lzx.article.feign.ESClient;
import com.lzx.article.feign.UserClient;
import com.lzx.article.mapper.ArticlesMapper;
import com.lzx.article.mapper.CollectMapper;
import com.lzx.article.service.CollectService;
import com.lzx.article.vo.User;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Resource
    private ArticlesMapper articlesMapper;

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private UserClient userClient;

    @Resource
    private ESClient esClient;

    //收藏功能
    @GlobalTransactional
    @Override
    public R saveUserCollect(Collect collect) {
        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id和文章id查找收藏表是否有相应记录
        queryWrapper.eq(Collect::getArticleId, collect.getArticleId()).eq(Collect::getUserId, collect.getUserId());
        Collect vo = collectMapper.selectOne(queryWrapper);
        if (vo != null) {
            vo.setCreateTime(new Date());
            if (vo.getIsCollect() == 0) {
                vo.setIsCollect(1);
                collectMapper.updateById(vo);
                //收藏，则文章收藏数+1
                LambdaQueryWrapper<Articles> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Articles::getId, collect.getArticleId());
                Articles article = articlesMapper.selectOne(wrapper);
                article.setCollectCount(article.getCollectCount() + 1);
                articlesMapper.updateById(article);
                //es同步
                esClient.editArticle(Long.valueOf(collect.getArticleId()));
                return R.ok().message("收藏成功");
            } else {
                vo.setIsCollect(0);
                collectMapper.updateById(vo);
                //取消收藏，则文章收藏数-1
                LambdaQueryWrapper<Articles> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Articles::getId, collect.getArticleId());
                Articles article = articlesMapper.selectOne(wrapper);
                article.setCollectCount(article.getCollectCount() - 1);
                articlesMapper.updateById(article);
                //es同步
                esClient.editArticle(Long.valueOf(collect.getArticleId()));
                return R.ok().message("取消收藏");
            }
        } else {
            //如果没有收藏记录
            collect.setCreateTime(new Date());
            collect.setIsCollect(1);
            collectMapper.insert(collect);
            //收藏，则文章收藏数+1
            LambdaQueryWrapper<Articles> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Articles::getId, collect.getArticleId());
            Articles article = articlesMapper.selectOne(wrapper);
            article.setCollectCount(article.getCollectCount() + 1);
            articlesMapper.update(article, wrapper);
            //es同步
            esClient.editArticle(Long.valueOf(collect.getArticleId()));

            //发送通知
            Inform inform = new Inform();
            inform.setUserId(article.getUserId());
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(collect.getUserId()));
            inform.setSender(collect.getUserId());
            inform.setNickname(user.getNickname());
            inform.setAvatar(user.getAvatar());
            inform.setType(InformType.COLLECT.getType());
            inform.setMessage(article.getTitle());
            userClient.saveInform(inform);

            return R.ok().message("收藏成功");
        }
    }

    //查询当前用户的收藏记录
    @Override
    public R getUserCollectList(int userId) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Collect> collectList = collectMapper.selectList(wrapper);
        for (Collect collect : collectList) {
            Articles articles = articlesMapper.selectById(collect.getArticleId());
            articles.setLabels(Arrays.asList(articles.getLabelTranslation().split(",")));
            collect.setArticles(articles);
        }
        return R.ok().data("list", collectList);
    }
}
